package com.cskaoyan.javase.thread._2api;

/**
 * 牛刀小试：
 *
 * 给定三个线程A、B、C，其中：
 *
 * A线程会每一秒输出一次“hello world!”，一共输出5次
 * B线程会每一秒输出一次“天亮之前说再见!”，一共输出5次
 * C线程会每一秒输出一次“我真的好想再活五百年!”，一共输出5次
 * 要求利用线程控制API实现：
 *
 * A线程执行完毕后，B线程紧接着执行，B线程执行完毕后，C线程紧接着执行。
 *
 * @since 17:08
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class ExerciseJoinDemo {
    public static void main(String[] args) {
        ThreadA ta = new ThreadA();
        ThreadB tb = new ThreadB();
        ThreadC tc = new ThreadC();
        ta.start();
        try {
            ta.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tb.start();
        try {
            tb.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tc.start();
    }

    static class ThreadA extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println("hello world!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class ThreadB extends Thread {
        @Override
        public void run() {
            /*ThreadA ta = new ThreadA();
            ta.start();
            try {
                ta.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            for (int i = 0; i < 5; i++) {
                System.out.println("天亮之前说再见");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class ThreadC extends Thread {
        @Override
        public void run() {
            /*ThreadB tb = new ThreadB();
            tb.start();
            try {
                tb.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            for (int i = 0; i < 5; i++) {
                System.out.println("我真的好想再活五百年!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
